package com.cxb.view;

import java.util.Random;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.animation.AccelerateInterpolator;
import android.widget.ImageView;

import com.cxb.R;
import com.cxb.manager.CxbEventBus;

public class CompassView3 extends ImageView {

	private Drawable compassNeedle;
	private Handler mHandler;
	private AccelerateInterpolator accelerateInterpolator;

	private float targetDegree = 0;
	private float currentDegree = 0;
	private boolean isStartRoate = true;
	private CompassUpdater compassUpdater;
	private OnCompassDataUpdate onCompassDataUpdate;

	private boolean isShowCompass = false;

	public CompassView3(Context context, AttributeSet attrs) {

		super(context, attrs);
		compassNeedle = null;
		// this.setBackgroundResource(R.drawable.compass_bg);
		accelerateInterpolator = new AccelerateInterpolator();
		mHandler = new Handler();
		compassUpdater = new CompassUpdater();
		startRotate();
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {

			if (event.getAction() == MotionEvent.ACTION_DOWN) {
				CxbEventBus.getCxbBus(CxbEventBus.EVENT_GPS_CHANGE).post(
						new Random().nextInt(360));
			}

		return super.onTouchEvent(event);
	}

	public void setOnCompassDateUpdateListener(
			OnCompassDataUpdate onCompassDataUpdate) {
		this.onCompassDataUpdate = onCompassDataUpdate;
	}

	public void startRotate() {
		isStartRoate = true;
		mHandler.post(compassUpdater);
	}

	public void stopRotate() {
		isStartRoate = false;
	}

	public void setShowCompass(boolean isShow) {

	}

	public void rotateCompass(float degree) {

		if(degree<0){
			return;
		}
		int rate=(int) Math.round(degree/45.0);
		degree=rate*45;
		float to = degree;
		if (to - targetDegree > 180) {
			to -= 360;
		} else if (to - targetDegree < -180) {
			to += 360;
		}
		Log.i("test", "degree=" + degree);
		targetDegree = normalizeDegree(to);

		if (onCompassDataUpdate != null) {
			onCompassDataUpdate.onCompassUpdate(targetDegree);
		}
	}

	@Override
	protected void onDraw(Canvas canvas) {

		if (compassNeedle == null) {
			compassNeedle = getDrawable();
			compassNeedle.setBounds(0, 0, getWidth(), getHeight());
		}
		canvas.save();
		canvas.rotate(currentDegree, getWidth() / 2, getHeight() / 2);// ��ͼƬ���ĵ���ת��
		compassNeedle.draw(canvas);// ����ת���ͼƬ����view�ϣ���������ת�������
		canvas.restore();// ����һ��
		// super.onDraw(canvas);
	}

	private float normalizeDegree(float degree) {
		return (degree + 720) % 360;
	}

	class CompassUpdater implements Runnable {
		@Override
		public void run() {
			if (isStartRoate && compassNeedle != null) {

				if (currentDegree != targetDegree) {
					float to = 360-targetDegree;
					if (to - currentDegree > 180) {
						to -= 360;
					} else if (to - currentDegree < -180) {
						to += 360;
					}
					currentDegree = normalizeDegree(currentDegree
							+ ((to - currentDegree) * accelerateInterpolator
									.getInterpolation(Math.abs(to
											- currentDegree) > 360 ? 0.4f
											: 0.3f)));// ����һ�����ٶ���ȥ��תͼƬ����ϸ��

					invalidate();
				}

			}
			if (isStartRoate) {
				mHandler.postDelayed(compassUpdater, 20);
			}

		}
	}

	public interface OnCompassDataUpdate {
		void onCompassUpdate(float degree);
	}

}
